set(SWIG_JAVA_SIMBODY_INTERFACE_FILE 
    "${CMAKE_CURRENT_SOURCE_DIR}/swig/java_simbody.i")
set(SWIG_JAVA_SIMBODY_CXX_FILE 
    "${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/java_simbody.cxx")
set(SWIG_JAVA_SIMBODY_H_FILE   
    "${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/java_simbody.h")

set(SWIG_JAVA_COMMON_INTERFACE_FILE 
    "${CMAKE_CURRENT_SOURCE_DIR}/swig/java_common.i")
set(SWIG_JAVA_COMMON_CXX_FILE 
    "${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/java_common.cxx")
set(SWIG_JAVA_COMMON_H_FILE   
    "${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/java_common.h")

set(SWIG_JAVA_SIMULATION_INTERFACE_FILE 
    "${CMAKE_CURRENT_SOURCE_DIR}/swig/java_simulation.i")
set(SWIG_JAVA_SIMULATION_CXX_FILE 
    "${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/java_simulation.cxx")
set(SWIG_JAVA_SIMULATION_H_FILE   
    "${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/java_simulation.h")

set(SWIG_JAVA_ACTUATORS_INTERFACE_FILE 
    "${CMAKE_CURRENT_SOURCE_DIR}/swig/java_actuators.i")
set(SWIG_JAVA_ACTUATORS_CXX_FILE 
    "${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/java_actuators.cxx")
set(SWIG_JAVA_ACTUATORS_H_FILE   
    "${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/java_actuators.h")

set(SWIG_JAVA_EXAMPLECOMPONENTS_INTERFACE_FILE 
    "${CMAKE_CURRENT_SOURCE_DIR}/swig/java_examplecomponents.i")
set(SWIG_JAVA_EXAMPLECOMPONENTS_CXX_FILE 
    "${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/java_examplecomponents.cxx")
set(SWIG_JAVA_EXAMPLECOMPONENTS_H_FILE   
    "${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/java_examplecomponents.h")

set(OPENSIM_JAVA_WRAPPING_PACKAGE "org.opensim.modeling"
    CACHE STRING
    "The wrapping is built as the Java package specified in this variable.")

# Replace periods with slashes (to make a path).
# Syntax for find-replace:
# string(REGEX REPLACE <pattern> <replacement string>
#                      <target variable> <source string>)
string(REGEX REPLACE "\\." "/"
       SWIG_JAVA_PACKAGE_PATH ${OPENSIM_JAVA_WRAPPING_PACKAGE})
string(REGEX REPLACE "\\." "-"
       SWIG_JAVA_JAR_NAME ${OPENSIM_JAVA_WRAPPING_PACKAGE})
set(SWIG_JAVA_JAR_NAME "${SWIG_JAVA_JAR_NAME}.jar"
    CACHE INTERNAL
    "Name of the jar file generated by archving SWIG generated Java files.")

set(SWIG_JAVA_SOURCE_BUILD_OUTPUT_DIR
    ${CMAKE_CURRENT_BINARY_DIR}/src/${SWIG_JAVA_PACKAGE_PATH})
set(SWIG_JAVA_JAR_BUILD_OUTPUT_PATH
    "${CMAKE_CURRENT_BINARY_DIR}/src/${SWIG_JAVA_JAR_NAME}")

# We place the .java files in this folder.
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory
    ${SWIG_JAVA_SOURCE_BUILD_OUTPUT_DIR})

# The actual SWIG command is in the OpenSimJNI folder, since the custom command
# must be defined in the same directory that uses it (for the osimJavaJNI
# target).

# Compile the C++ wrapper into a library.
# ---------------------------------------
add_subdirectory(OpenSimJNI)

# Compile java sources.
# ---------------------
find_package(Java 1.7 REQUIRED)

# To avoid compiling stale .java files, delete any existing files and copy
# over the new files from the module-specific directories (see
# OpenSimJNI/CMakeLists.txt).
add_custom_command(
    OUTPUT "${SWIG_JAVA_JAR_BUILD_OUTPUT_PATH}"
    DEPENDS ${SWIG_JAVA_ACTUATORS_CXX_FILE}
            ${SWIG_JAVA_EXAMPLECOMPONENTS_CXX_FILE}
    COMMAND ${CMAKE_COMMAND} -E remove ${SWIG_JAVA_SOURCE_BUILD_OUTPUT_DIR}/*
    COMMAND ${CMAKE_COMMAND} -E copy_directory
            ${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/src/simbody
            ${SWIG_JAVA_SOURCE_BUILD_OUTPUT_DIR}
    COMMAND ${CMAKE_COMMAND} -E copy_directory
            ${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/src/common
            ${SWIG_JAVA_SOURCE_BUILD_OUTPUT_DIR}
    COMMAND ${CMAKE_COMMAND} -E copy_directory
            ${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/src/simulation
            ${SWIG_JAVA_SOURCE_BUILD_OUTPUT_DIR}
    COMMAND ${CMAKE_COMMAND} -E copy_directory
            ${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/src/actuators-analysis-tools
            ${SWIG_JAVA_SOURCE_BUILD_OUTPUT_DIR}
    COMMAND ${CMAKE_COMMAND} -E copy_directory
            ${CMAKE_CURRENT_BINARY_DIR}/OpenSimJNI/src/examplecomponents
            ${SWIG_JAVA_SOURCE_BUILD_OUTPUT_DIR}
    COMMAND ${JAVA_COMPILE} 
            org/opensim/modeling/*.java 
            -source 1.6 -target 1.6
    COMMAND ${JAVA_ARCHIVE} -cvf ${SWIG_JAVA_JAR_NAME}
            org/opensim/modeling/*.class
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/src
    COMMENT "Compiling Java sources and creating jar archive.")

# Umbrella target for assembling the java bindings in the build tree.
# -------------------------------------------------------------------
add_custom_target(JavaBindings ALL
    DEPENDS "${SWIG_JAVA_JAR_BUILD_OUTPUT_PATH}"
    )

add_dependencies(JavaBindings osimJavaJNI)

set_target_properties(JavaBindings PROPERTIES
    PROJECT_LABEL "Java - umbrella target"
    FOLDER "Bindings")

# Install.
# --------
install(FILES "${SWIG_JAVA_JAR_BUILD_OUTPUT_PATH}"
    DESTINATION ${OPENSIM_INSTALL_JAVAJARDIR})


# Java tests.
# -----------

if(BUILD_TESTING)
    # Now actually add the tests.
    add_subdirectory(tests)
endif()

# MATLAB tests and installing.
# ----------------------------
add_subdirectory(Matlab)
